package com.example.administrator.zyfypt_211dsh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.zyfypt_211dsh.R;
import com.example.administrator.zyfypt_211dsh.bean.CollectionBean;
import com.example.administrator.zyfypt_211dsh.bean.SessionID;
import com.example.administrator.zyfypt_211dsh.bean.TcaseBean;
import com.example.administrator.zyfypt_211dsh.iface.CollectionListener;
import com.example.administrator.zyfypt_211dsh.model.CollectionModel_tcase;


import java.util.List;

import java.util.List;


/**
 * Created by Raindy on 2017/4/12.
 */

public class CollectionAdapter_tcase extends RecyclerView.Adapter<CollectionAdapter_tcase.ViewHolder>{
    private Context context;
    private LayoutInflater layoutInflater;
    private List<CollectionBean<TcaseBean>> list;
    private int count = 9;

    CollectionListener<TcaseBean> collectionListener_tcase=new CollectionListener<TcaseBean>() {
        @Override
        public void onResponse(List<CollectionBean<TcaseBean>> beanlist) {
            list.addAll(beanlist);
            notifyDataSetChanged();
            Toast.makeText(context, "items:" + getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            //Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    public CollectionAdapter_tcase(Context context) {
        this.context=context;
        layoutInflater= LayoutInflater.from(this.context);

    }
    public void setList(List<CollectionBean<TcaseBean>> list){
        this.list=list;
    }
    public class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView imageView;
        TextView tvtitle,tvdescript,tvtime;
        Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            //tvdescript=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
            button=(Button)itemView.findViewById(R.id.button);

        }
    }
    //为了每个Item生成对应的View

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }
    //为ViewHolder中的控件设置填充值，设置监听
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if(position == count){
            CollectionModel_tcase model= new CollectionModel_tcase();
            model.getResultList("tcase",(((count+1)/10)+1),SessionID.sessionID,collectionListener_tcase);
            Toast.makeText(context, "page:" + (((count+1)/10)+1), Toast.LENGTH_SHORT).show();
            count+=10;
        }
        CollectionBean<TcaseBean> collectionBean=list.get(position);//取出list一条数据
        if (collectionBean==null)
            return;
        //holder.imageView.setImageResource(articleBean.getImgid());
        holder.tvtitle.setText(collectionBean.getResid());
        //holder.tvdescript.setText(articleBean.getDescription());
        holder.tvtime.setText(collectionBean.getCctime());
//        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+collectionBean.getThumb()).placeholder(R.mipmap.ic_launcher).into(holder.imageView);
        //给条目中的按钮设置监听器
        holder.button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                               @Override
                                               public void onClick(View v) {
                                                   Toast.makeText(context, list.get(position).getId(), Toast.LENGTH_SHORT).show();
                                               }
                                           }


        );
    }
    @Override
    public int getItemCount()
    {
        if(list==null) return 0;
        else
            return list.size();
    }
}